package com.sdkj.controller.law;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.sdkj.base.common.FileDownload;
import com.sdkj.base.common.FileUpload;
import com.sdkj.controller.base.BaseController;
import com.sdkj.entity.Page;
import com.sdkj.service.law.LawProtocolService;
import com.sdkj.util.AppUtil;
import com.sdkj.util.Const;
import com.sdkj.util.DateUtil;
import com.sdkj.util.Jurisdiction;
import com.sdkj.util.ObjectExcelView;
import com.sdkj.util.PageData;
import com.sdkj.util.PathUtil;



/** 
 * 类名称：LawInfoController
 * 创建人：hxf 
 * 创建时间：2017-05-23
 */
@Controller
@RequestMapping(value="/lawprotocol")
public class LawProtocolController extends BaseController{
	String menuUrl = "lawprotocol/list.do"; //菜单地址(权限用)
	@Resource(name="lawProtocolService")
	private LawProtocolService lawProtocolService;
	/*
	 * 列表
	 */
	@RequestMapping(value="/list")
	public ModelAndView list(Page page){
		logBefore(logger, "列表lawprotocol");
		ModelAndView mv = this.getModelAndView();
		PageData  pd = new PageData();
		try {
			pd = this.getPageData();
			String DIM = pd.getString("DIM");
			String Status = pd.getString("STATUS");
			if(DIM != null && DIM.equals("")){
				DIM = DIM.trim();
				pd.put("DIM", DIM);
			}
			pd.put("STATUS", Status);
			page.setPd(pd);
			List<PageData> varlList = lawProtocolService.list(page);
			mv.setViewName("law/lawProtocol/lawProtocol_list");
			mv.addObject("varList", varlList);
			mv.addObject("pd", pd);	
			mv.addObject(Const.SESSION_QX, this.getHC());
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	/*
	 *新增 
	 */
	@RequestMapping(value="/goAdd")
	public ModelAndView goAdd(){
		logBefore(logger, "去新增lawprotocol页面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			mv.setViewName("law/lawProtocol/lawProtocol_add");
			mv.addObject("msg", "save");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}
	
	/*
	 * 保存
	 */
	@RequestMapping(value="/save")
	public ModelAndView save(MultipartFile file,HttpServletRequest request) throws Exception{
		logBefore(logger, "新增lawprotocol");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData(request);
		String time = DateUtil.getDay();
		String filePath = null;
		String fileName = null;
		if (null != file && !file.isEmpty()) {
			filePath =PathUtil.getClasspath()+ Const.FILEPATHFILE+time;
			fileName = FileUpload.fileUpload(file, filePath, this.get32UUID());	
		}else {
			System.out.println("上传失败");
		}
		pd.put("protocol_id", this.get32UUID());	//主键
		pd.put("user_id", getCurrentUser().getUSER_ID());
		pd.put("protocol_person", getCurrentUser().getUSERNAME());
		pd.put("dept_id", getCurrentUser().getDepartmentId());
		pd.put("company_id", getCurrentUser().getCompanyId());
		pd.put("protocol_attachment",filePath);
		pd.put("protocol_name", fileName);
		lawProtocolService.save(pd);
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}
	
	
	/**
	 * 去修改页面
	 */
	@RequestMapping(value="/goEdit")
	public ModelAndView goEdit(){
		logBefore(logger, "去lawprotocol页面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			pd = lawProtocolService.findById(pd);	//根据ID读取
			mv.setViewName("law/lawProtocol/lawProtocol_edit");
			mv.addObject("msg", "edit");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}	
	/*
	 * 修改
	 * edit
	 */
	@RequestMapping(value="/edit")
	public ModelAndView edit() throws Exception{
		logBefore(logger, "修改lawprotocol");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		lawProtocolService.edit(pd);
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}
	/*
	 * 禁用
	 */
	@RequestMapping(value="/delete")
	public ModelAndView delete() throws Exception{
		logBefore(logger, "禁用lawprotocol");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		lawProtocolService.update(pd);
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}
	/*
	 * 下载功能
	 */
	@RequestMapping(value="download")
	public void download(HttpServletResponse response)throws Exception{
		logBefore(logger, "下载功能");
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "edit")) {return;}
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		pd = lawProtocolService.findById(pd);
		String filePath = (String) pd.get("protocol_attachment");
		String fileName = (String) pd.get("protocol_name");
		FileDownload.fileDownload(response, filePath+"/"+fileName, fileName);
	}
	/*
	 * 批量禁用
	 */
	@RequestMapping(value="/updaetAll")
	@ResponseBody
	public Object updaetAll() {
		logBefore(logger, "批量禁用lawprotocol");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "dell")){return null;} //校验权限
		PageData pd = new PageData();		
		Map<String,Object> map = new HashMap<String, Object>();
		try {
			pd = this.getPageData();
			List<PageData> pdList = new ArrayList<PageData>();
			String DATA_IDS = pd.getString("DATA_IDS");
			if(null != DATA_IDS && !"".equals(DATA_IDS)){
				String ArrayDATA_IDS[] = DATA_IDS.split(",");
				lawProtocolService.updateAll(ArrayDATA_IDS);
				pd.put("msg", "ok");
			}else{
				pd.put("msg", "no");
			}
			pdList.add(pd);
			map.put("list", pdList);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			logAfter(logger);
		}
		return AppUtil.returnObject(pd, map);
	}
	/*
	 * 导出到excel
	 * @return
	 */
	@RequestMapping(value="/excel")
	public ModelAndView exportExcel(){
		logBefore(logger, "导出lawprotocol到excel");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "cha")){return null;}
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try{
			Map<String,Object> dataMap = new HashMap<String,Object>();
			List<String> titles = new ArrayList<String>();
			titles.add("");	//1
			titles.add("");	//2
			titles.add("");	//3
			titles.add("");	//4
			titles.add("");	//5
			titles.add("");	//6
			titles.add("");	//7
			dataMap.put("titles", titles);
			List<PageData> varOList = lawProtocolService.listAll(pd);
			List<PageData> varList = new ArrayList<PageData>();
			for(int i=0;i<varOList.size();i++){
				PageData vpd = new PageData();
				vpd.put("var1", varOList.get(i).getString("protocol_id"));	//1
				vpd.put("var2", varOList.get(i).getString("protocol_dept"));	//2
				vpd.put("var3", varOList.get(i).getString("protocol_person"));	//3
				vpd.put("var4", varOList.get(i).getString("protocol_title"));	//4
				vpd.put("var5", varOList.get(i).getString("protocol_remark"));	//5
				vpd.put("var6", varOList.get(i).getString("protocol_attachment"));	//6
				vpd.put("var7", varOList.get(i).getString("protocol_status"));	//7
				varList.add(vpd);
			}
			dataMap.put("varList", varList);
			ObjectExcelView erv = new ObjectExcelView();
			mv = new ModelAndView(erv,dataMap);
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	
	
	
	
	/* ===============================权限================================== */
	public Map<String, String> getHC(){
		Subject currentUser = SecurityUtils.getSubject();  //shiro管理的session
		Session session = currentUser.getSession();
		return (Map<String, String>)session.getAttribute(Const.SESSION_QX);
	}
/* ===============================权限================================== */
	
	@InitBinder
	public void initBinder(WebDataBinder binder){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(format,true));
	}


}
